package middleware

import (
	"github.com/gin-contrib/sessions"
	"github.com/gin-gonic/gin"
)

func LoginMiddleWare() gin.HandlerFunc {
	return func(c *gin.Context) {
		session := sessions.Default(c)
		userName := session.Get("userName")
		userId := session.Get("userId")
		if _, ok := userName.(string); !ok {
			c.AbortWithStatusJSON(401, gin.H{
				"retCode": 401,
				"msg":     "请先登录",
				"data":    nil,
			})
			return
		}
		c.Keys["userName"] = userName
		c.Keys["userId"] = userId
	}
}
